﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Hsp.WebNovels.Common.Constants
{
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public static class EnumsHelper
    {
        public static T? GetAttributeOfType<T>(this Enum enumVal) where T : Attribute
        {
            try
            {
                var typeInfo = enumVal.GetType().GetTypeInfo();
                var mi = typeInfo.DeclaredMembers.First(x => x.Name == enumVal.ToString());
                return mi.GetCustomAttribute<T>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static string? GetName(this Enum enumVal)
        {
            var attr = GetAttributeOfType<DisplayAttribute>(enumVal);
            return attr != null ? attr.Name : string.Empty;
        }

        /// <summary>
        /// 生成枚举数据集
        /// </summary>
        /// <typeparam name="T">T is Enum</typeparam>
        /// <returns></returns>
        public static IDictionary<string, object> Transfer<T>()
        {
            Type type = typeof(T);
            if (!type.IsEnum)
                throw new ArgumentOutOfRangeException("T is not a Enum");

            IDictionary<string, object> dict = new Dictionary<string, object>();

            foreach (FieldInfo fInfo in type.GetFields())
            {
                foreach (Attribute attr in fInfo.GetCustomAttributes(false))
                {
                    if (attr is DescriptionAttribute dAttr)
                    {
                        int enumValue = Convert.ToInt32(Enum.Parse(type, fInfo.Name));
                        dict.Add(dAttr.Description, enumValue);
                    }
                }
            }

            return dict;
        }
    }


    /// <summary>
    /// 地址是否需要组合：0-不需要；1-网站地址 + 章节地址；2-小说地址 + 章节地址；
    /// </summary>
    public enum EnumUrlCombine
    {
        /// <summary>
        /// 不需要
        /// </summary>
        [Display(Name = "不需要")]
        不需要 = 0,
        /// <summary>
        /// 网站地址 + 章节地址
        /// </summary>
        [Display(Name = "网站地址 + 章节地址")]
        网站地址加章节地址 = 1,
        /// <summary>
        /// 小说地址 + 章节地址
        /// </summary>
        [Display(Name = "小说地址 + 章节地址")]
        小说地址加章节地址 = 2,
    }

    /// <summary>
    /// 是否有效：0-否；1-是；
    /// </summary>
    public enum EnumValid
    {
        /// <summary>
        /// 否
        /// </summary>
        [Display(Name = "否")]
        否 = 0,
        /// <summary>
        /// 是
        /// </summary>
        [Display(Name = "是")]
        是 = 1,
    }

    /// <summary>
    /// 小说状态：0-连载中；1-已完本；2-已太监；
    /// </summary>
    public enum EnumStatus
    {
        /// <summary>
        /// 连载中
        /// </summary>
        [Display(Name = "连载中")]
        连载中 = 0,
        /// <summary>
        /// 已完本
        /// </summary>
        [Display(Name = "已完本")]
        已完本 = 1,
        /// <summary>
        /// 已太监
        /// </summary>
        [Display(Name = "已太监")]
        已太监 = 2,
    }

    /// <summary>
    /// 已删除：0-否；1-是；
    /// </summary>
    public enum EnumDeleted
    {
        /// <summary>
        /// 否
        /// </summary>
        [Display(Name = "否")]
        否 = 0,
        /// <summary>
        /// 是
        /// </summary>
        [Display(Name = "是")]
        是 = 1,
    }

    /// <summary>
    /// 有效章节：0-否；1-是；
    /// </summary>
    public enum EnumValidChapter
    {
        /// <summary>
        /// 否
        /// </summary>
        [Display(Name = "否")]
        否 = 0,
        /// <summary>
        /// 是
        /// </summary>
        [Display(Name = "是")]
        是 = 1,
    }

    /// <summary>
    /// 正文类型：0-文本；1-HTML；
    /// </summary>
    public enum EnumAnnotationType
    {
        /// <summary>
        /// 文本
        /// </summary>
        [Display(Name = "文本")]
        文本 = 0,
        /// <summary>
        /// HTML
        /// </summary>
        [Display(Name = "HTML")]
        HTML = 1,
    }







}
